/*
 * Alloy Analyzer 4 -- Copyright (c) 2006-2008, Felix Chang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.ut.ece382c.valloy.ast;

import edu.mit.csail.sdg.alloy4.Pos;
import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4.ErrorType;
import edu.mit.csail.sdg.alloy4.ConstList;
import static edu.ut.ece382c.valloy.ast.Type.EMPTY;

/**
 * Immutable; represents an unresolved node that has several possibilities.
 */

public final class ExprChoice extends Expr {

    /** The unmodifiable list of Expr(s) from that this ExprChoice can refer to. */
    public final ConstList<Expr> choices;

    /** The unmodifiable list of String(s) explaining where each choice came from. */
    public final ConstList<String> reasons;

    /** Caches the span() result. */
    private Pos span=null;

    //============================================================================================================//

    /** {@inheritDoc} */
    @Override public Pos span() {
        Pos p=span;
        if (p==null) {
            p=pos;
            for(Expr a:choices) p=p.merge(a.span());
            span=p;
        }
        return p;
    }

    //============================================================================================================//

    /** {@inheritDoc} */
    @Override public void toString(StringBuilder out, int indent, boolean newline) {
    	for(int i=0; i<indent && newline; i++) { out.append(' '); }
    	out.append("<");
        for(Expr e:choices) { e.toString(out, indent, false); out.append(";"); }
        out.append(">");
    }


    //============================================================================================================//

    /** Constructs an ExprChoice node. */
    private ExprChoice(Pos pos, ConstList<Expr> choices, ConstList<String> reasons, Type type, long weight) {
        super(pos, null, true, type, 0, weight, emptyListOfErrors);
    	this.choices = choices;
        this.reasons = reasons;
    }

    //============================================================================================================//

    /** Construct an ExprChoice node. */
    public static Expr make(Pos pos, ConstList<Expr> choices, ConstList<String> reasons) {
    	return new ExprChoice(pos, choices, reasons, EMPTY, 0);
    }

    //============================================================================================================//

    
    /** {@inheritDoc} */
    public int getDepth() {
        int max = 1;
        for(Expr x: choices) { int tmp=x.getDepth(); if (max<tmp) max=tmp; }
        return 1 + max;
    }

    /** {@inheritDoc} */
    @Override final<T> T accept(VisitReturn<T> visitor) throws Err {
        if (!errors.isEmpty()) throw errors.pick();
        throw new ErrorType(span(), "This expression failed to be resolved.");
    }
}
